<?php

namespace App\Models\Mgt;

use Illuminate\Database\Eloquent\Model;
use Laracasts\Presenter\PresentableTrait;

//use GeneaLabs\LaravelModelCaching\Traits\Cachable;

class Activity extends Model
{
    use PresentableTrait;

    protected $table = 'biz_activities';

    protected $presenter = 'App\Http\Presenters\ActivityPresenter';

    protected $guarded = array();

    public $timestamps = false;

    const CHECKIN_ENABLE = 1;
    const CHECKIN_DISABLE = 0;    

    const SIGNIN_ENABLE = 1;
    const SIGNIN_DISABLE = 0;

    /*
    签到数
     */
    public function tracks()
    {
        return $this->hasMany('\App\Models\Biz\Track', 'aid', 'id');
    }

    public function branch()
    {
        return $this->belongsTo('\App\Models\Mgt\Branch', 'branch_id', 'id');
    }

    /*
    借阅数
     */
    public function borrows()
    {
        return $this->hasMany('\App\Models\Biz\Borrow', 'aid', 'id');
    }

    /*
    归还数
     */
    public function returns()
    {
        return $this->hasMany('\App\Models\Biz\Borrow', 'rid', 'id');
    }

    //常规活动参与者
    public function joins()
    {
        return $this->tracks()->reader()->orderBy('date', 'DESC');
    }

    //常规活动志愿者
    public function acts()
    {
        return $this->tracks()->volunteer()->orderBy('date', 'DESC');
    }


    //报名活动签到数
    public function checks()
    {
        return $this->tracks()
            ->where('aid', $this->id)
            ->where('sign', 1)
            ->where('check', 1)
            ->orderBy('date', 'DESC');
    }

    //报名活动报名者
    public function reader()
    {
        return $this->tracks()
            ->where('aid', $this->id)
            ->where('sign', 1)
            ->where('type', 1)
            ->orderBy('date', 'DESC');
    }

    //报名活动报名者
    public function volunteer()
    {
        return $this->tracks()
            ->where('aid', $this->id)
            ->where('sign', 1)
            ->where('type', 2)
            ->orderBy('date', 'DESC');
    }


    public static function current($sign = null)
    {
        if (isset($sign))
            return self::normal()->checkin()->signin()->orderBy('id', 'desc')
                        ->first();

        return self::withCount('tracks','joins','acts','borrows','returns')
                    ->where('is_checkin', self::CHECKIN_ENABLE)
                    //->where('is_signin', self::SIGNIN_DISABLE)
                    ->orderBy('id', 'desc')
                    ->first();
    }


    public static function nonCheck()
    {
        return self::normal()->noCheckin()->orderBy('id', 'desc')
                        ->first();
    }


    /**
     * 正常开放的活动
     * @param  [type] $query [description]
     * @return [type]        [description]
     */
    public function scopeNormal($query)
    {
        return $query->where('status', STATUS_NORMAL);
    }

    /**
     * 允许签到的活动
     * @param  [type] $query [description]
     * @return [type]        [description]
     */
    public function scopeCheckin($query)
    {
        return $query->where('is_checkin', STATUS_NORMAL);
    }


    /**
     * 不允许签到的活动
     * @param  [type] $query [description]
     * @return [type]        [description]
     */
    public function scopeNoCheckin($query)
    {
        return $query->where('is_checkin', STATUS_FREEZE);
    }

    /**
     * 允许报名的活动
     * @param  [type] $query [description]
     * @return [type]        [description]
     */
    public function scopeSignin($query)
    {
        return $query->where('is_signin', STATUS_NORMAL);
    }


    public static function enabled()
    {
        $result = self::where('is_checkin', self::CHECKIN_ENABLE)
                    ->where('date_begin','<',time())
                    ->where('date_end','>',time()-86400)
                    ->orderBy('id', 'desc')
                    ->first();

        return !!$result;
    }


    public function summary()
    {
        $result = array();

        $result["act_joins"] = $this->joins()->count();
        $result["act_acts"] = $this->acts()->count();
        $result["act_borrows"] = $this->borrows()->count();
        $result["act_returns"] = $this->returns()->count();

        return $result;
    }


    public function datasets($action)
    {
        switch ($action) {
            case 'joins':
                $datasets = $this->joins;
                break;
            case 'acts':
                $datasets = $this->acts;
                break;
            case 'borrows':
                $datasets = $this->borrows;
                break;
            case 'returns':
                $datasets = $this->returns;
                break;
            case 'checks':
                $datasets = $this->checks;
                break;
            case 'reader':
                $datasets = $this->reader;
                break;
            case 'volunteer':
                $datasets = $this->volunteer;
                break;
            default:
                break;
        }

        if ($datasets->count() > 0) {
            $data = $datasets->all();
            $data = collect($data)->map(function ($result) {
                $result->user->mobile = substr_replace($result->user->mobile, '****', 3, 4);
                $result->user->signed = $result->user->signed($this->id, $result->user->type);
                $result->user->awarded = $result->user->signed($this->id, 3);
                return $result;
            });
        }

        return $datasets;
    }

}